package com.qianchang.microfinance.view.credit.data;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.qianchang.microfinance.R;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.dm.library.adapter.CommonAdapter;
import com.dm.library.bean.ContractListBean;
import com.dm.library.utils.PhoneDataUtils;
import com.dm.library.widgets.indexlib.IndexBar.widget.DividerItemDecoration;
import com.dm.library.widgets.indexlib.IndexBar.widget.IndexBar;
import com.dm.library.widgets.indexlib.suspension.SuspensionDecoration;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述:
 * 修 改 人: zhoulai
 * 修改时间: 2017/8/12 11:31
 */


public class ContractListActivity extends BaseActivity implements CommonAdapter.OnItemClickListener {

    @BindView(R.id.rv_contract_list)
    RecyclerView rvContractList;
    @BindView(R.id.indexBar)
    IndexBar indexBar;
    @BindView(R.id.tvSideBarHint)
    TextView tvSideBarHint;
    private List<ContractListBean> contactsList;
    public static final String CONTRACT_BEAN = "contract_bean";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contract_list);
        ButterKnife.bind(this);
        initData();
    }

    @Override
    protected void initData() {
        super.initData();
        checkPermission(new CheckPermListener() {
            @Override
            public void superPermission() {
                show();
                contactsList = PhoneDataUtils.getContactsList(ContractListActivity.this);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ContractListActivity.this);
                rvContractList.setLayoutManager(linearLayoutManager);
                rvContractList.addItemDecoration(new SuspensionDecoration(ContractListActivity.this, contactsList));
                rvContractList.addItemDecoration(new DividerItemDecoration(ContractListActivity.this, DividerItemDecoration.VERTICAL_LIST));
                ContractListAdapter contractListAdapter = new ContractListAdapter(ContractListActivity.this, contactsList);
                indexBar.setmPressedShowTextView(tvSideBarHint).setNeedRealIndex(true).setmLayoutManager(linearLayoutManager);
                indexBar.setmSourceDatas(contactsList)//设置数据
                        .invalidate();
                rvContractList.setAdapter(contractListAdapter);
                contractListAdapter.setOnItemClickListener(ContractListActivity.this);
                dismiss();
            }

            @Override
            public void permissionSomeDeny() {

            }
        }, getString(R.string.ask_again), Manifest.permission.READ_CONTACTS);
    }

    @Override
    public void onItemClick(View v, int position) {
        Intent intent = new Intent();
        intent.putExtra(CONTRACT_BEAN, contactsList.get(position));
        setResult(RESULT_OK, intent);
        finish();
    }
}
